package com.tsing.basic.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsing.basic.service.IUserService;
import com.tsing.basic.vo.req.*;
import com.tsing.basic.vo.resp.*;
import com.tsing.common.annotations.NotToken;
import com.tsing.common.annotations.SystemLog;
import com.tsing.common.utils.ResultUtil;
import com.tsing.common.vo.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/login")
    @NotToken
    @ApiOperation(value = "用户登录")
    @SystemLog(description = "用户登录")
    public ResultData<UserLoginRespVo> login(@Validated @RequestBody UserLoginReqVo reqVo) {
        return ResultUtil.success(userService.login(reqVo));
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出登录")
    @SystemLog(description = "退出登录")
    public ResultData logout(HttpServletRequest request) {
        userService.logout(request);
        return ResultUtil.success();
    }

    @GetMapping("/getHouseTree")
    @ApiOperation(value = "获取房屋树")
    @SystemLog(description = "获取房屋树")
    public ResultData<List<HouseTreeRespVo>> getHouseTree() {
        return ResultUtil.success(userService.getHouseTree());
    }

    @GetMapping("/getCarportTree")
    @ApiOperation(value = "获取停车位树")
    @SystemLog(description = "获取停车位树")
    public ResultData<List<CarportTreeRespVo>> getCarportTree() {
        return ResultUtil.success(userService.getCarportTree());
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "新增用户")
    @SystemLog(description = "新增用户")
    public ResultData addUser(@Validated @RequestBody UserAddReqVo reqVo) {
        userService.addUser(reqVo);
        return ResultUtil.success();
    }

    @DeleteMapping("/deleteUser")
    @ApiOperation(value = "删除用户")
    @SystemLog(description = "删除用户")
    public ResultData deleteUser(@Validated @RequestBody IdsReqVo reqVo) {
        userService.deleteUser(reqVo);
        return ResultUtil.success();
    }

    @GetMapping("/getUser")
    @ApiOperation(value = "获取单个用户信息")
    @SystemLog(description = "获取单个用户信息")
    public ResultData<UserRespVo> getUserById(@Validated IdReqVo reqVo) {
        return ResultUtil.success(userService.getUserById(reqVo));
    }

    @PutMapping("/updateUser")
    @ApiOperation(value = "修改用户信息")
    @SystemLog(description = "修改用户信息")
    public ResultData updateUser(@Validated @RequestBody UserUpdateReqVo reqVo) {
        userService.updateUser(reqVo);
        return ResultUtil.success();
    }

    @PutMapping("/initUserPw")
    @ApiOperation(value = "初始化用户密码")
    @SystemLog(description = "初始化用户密码")
    public ResultData initUserPw(HttpServletRequest request) {
        userService.initUserPw(request);
        return ResultUtil.success();
    }

    @GetMapping("/passwordVerify")
    @ApiOperation(value = "修改密码验证")
    @SystemLog(description = "修改密码验证")
    public ResultData<String> passwordVerify(HttpServletRequest request, @Validated PasswordVerifyReqVo reqVo) {
        return ResultUtil.success(userService.passwordVerify(request, reqVo));
    }

    @PutMapping("/updatePassword")
    @ApiOperation(value = "修改用户密码")
    @SystemLog(description = "修改用户密码")
    public ResultData updatePassword(HttpServletRequest request, @Validated @RequestBody UpdatePasswordReqVo reqVo) {
        userService.updatePassword(request, reqVo);
        return ResultUtil.success();
    }

    @GetMapping("/userPage")
    @ApiOperation(value = "用户分页")
    @SystemLog(description = "用户分页")
    public ResultData<Page<UserRespVo>> userPage(@Validated UserPageReqVo reqVo) {
        return ResultUtil.success(userService.userPage(reqVo));
    }

    @GetMapping("/getRole")
    @ApiOperation(value = "获取单个用户角色")
    @SystemLog(description = "获取单个用户角色")
    public ResultData<List<RoleRespVo>> getRoleById(@Validated IdReqVo reqVo) {
        return ResultUtil.success(userService.getRoleById(reqVo));
    }

    @GetMapping("/getRoleList")
    @ApiOperation(value = "获取角色列表")
    @SystemLog(description = "获取角色列表")
    public ResultData<List<RoleRespVo>> getRoleList() {
        return ResultUtil.success(userService.getRoleList());
    }

    @PostMapping("/bindRole")
    @ApiOperation(value = "用户绑定角色")
    @SystemLog(description = "用户绑定角色")
    public ResultData bindRole(@Validated @RequestBody BindRoleReqVo reqVo) {
        userService.bindRole(reqVo);
        return ResultUtil.success();
    }

    @NotToken
    @PostMapping("/miniLogin")
    @ApiOperation(value = "小程序登录", notes = "错误代码为820时，请使用账号+密码登录")
    @SystemLog(description = "小程序登录")
    public ResultData<IdAndTokenRespVo> miniLogin(@Validated @RequestBody CodeReqVo reqVo) {
        return ResultUtil.success(userService.miniLogin(reqVo));
    }

    @NotToken
    @PostMapping("/miniPhoneLogin")
    @SystemLog(description = "小程序账号+密码登录")
    public ResultData<IdAndTokenRespVo> miniPhoneLogin(@Validated @RequestBody MiniPhoneLoginReqVo reqVo) {
        return ResultUtil.success(userService.miniPhoneLogin(reqVo));
    }

}
